package com.example.administrator.yuejian23.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian23.Bean.MyBean;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/18.
 */

public class SettingActivity extends Activity {
    private Retrofit retrofit;
    private Button edit_btn_return,edit_btn_save,edit_btn_username;
    private EditText edit_username;
    private EditText edit_password;
    private EditText sure_password;
    private Intent intent;
    private int user_id;
    private String user_name,user_password;
    private View itemView;
    private int userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_setting_info);

        edit_username= (EditText) findViewById(R.id.username);
        edit_password= (EditText) findViewById(R.id.edit_pass);
        sure_password= (EditText) findViewById(R.id.sure_pass);
        edit_btn_save= (Button) findViewById(R.id.save_pass);
        edit_btn_username= (Button) findViewById(R.id.save_name);
        edit_btn_return= (Button) findViewById(R.id.btn_return);
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("username","0");
        edit_btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        edit_btn_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_name =edit_username.getText().toString();
              /*  Toast.makeText(SettingActivity.this,"用户名为空请重新填写",Toast.LENGTH_LONG).show();*/
                if(str_name.equals("")){
                    Toast.makeText(SettingActivity.this,"用户名为空请重新填写",Toast.LENGTH_LONG).show();
                }else {
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(Const.BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    RetrofitService services = retrofit.create(RetrofitService.class);
                    Call<Integer> call = services.setName(user_id, edit_username.getText().toString());
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {

                            if (response.body() == 1) {
                                Toast.makeText(SettingActivity.this, "用户名修改成功", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(SettingActivity.this, "用户名修改失败", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });
        edit_btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService services=retrofit.create(RetrofitService.class);
                Call<Integer> call=services.setPassword(user_id,edit_password.getText().toString());
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Integer result = response.body();
                        String str_psd =edit_password.getText().toString();
                        String str_psdagain =sure_password.getText().toString();
                        if(str_psd.equals(str_psdagain)){
                            if(result==0){
                                Toast.makeText(SettingActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(SettingActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else {
                            Toast.makeText(SettingActivity.this,   "两次密码输入不相同", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });
        edit_username.setText(user_name);

    }
}